package com.song.ejf.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.song.ejf.dao.BaseDAO;
import com.song.ejf.page.Page;
import com.song.ejf.pojo.SRoleUser;
import com.song.ejf.pojo.SUser;
import com.song.ejf.service.UserService;
import com.song.ejf.util.DateUtil;
import com.song.ejf.util.UUIDUtil;

@Service("userService")
public class UserServiceImpl implements UserService{
	@Autowired
	BaseDAO baseDAO;
	/**
	 * 根据用户名和密码取用户对象
	 */
	public SUser getUserByUsernameAndPwd(String username,String password) throws Exception{
		if(StringUtils.isBlank(username) || StringUtils.isBlank(password)){
			return null;
		}
		String hql = " from SUser t where t.username = '" + username 
				+ "' and t.password = '" + password + "'";
		return (SUser)baseDAO.loadObject(hql);
	}

	@Override
	public void loadUserPage(Page page, String username, String name) throws Exception {
		if(username == null){
			username = "";
		}
		if(name == null){
			name = "";
		}
		Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("username", "%" + username.trim() + "%");
		paraMap.put("name", "%" + name.trim() + "%");
		String selectSql = "select u_id,username,name,last_modify_time  ";
		String mainSql = " select u.u_id,u.username,u.name,u.last_modify_time,rownum RN from s_user u " +
				"where 1=1 and u.name like :name and u.username like :username ";
		String order = " order by u.username";
//		String pageSql =  " where RN > " + page.getStartIndex() + " and RN < " + page.getEndIndex() + " ";
		baseDAO.sqlPageQuery(page, selectSql, mainSql, order, paraMap);
		
	}

	@Override
	public boolean delUser(String[] checkItems)  throws Exception{
		if(checkItems.length <= 0){
			return false;
		}
		for (int i = 0; i < checkItems.length; i++) {
			baseDAO.delById(SUser.class, checkItems[i]);
		}
		return true;
	}

	@Override
	public void saveOrUpdateUser(SUser user) throws Exception {
		baseDAO.saveOrUpdate(user);
	}

	@Override
	public SUser findUserById(String id)  throws Exception{
		return (SUser)baseDAO.loadById(SUser.class, id);
	}

	@Override
	public List<Map<String, String>> loadRoleByUserId(String uId) throws Exception {
		if(StringUtils.isBlank(uId)){
			return null;
		}
		String mainSql = "select sur.role_id From s_role sr,s_user_role sur where sr.r_id = sur.role_id and sur.user_id = :uId";
		Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("uId", uId);
		return baseDAO.sqlQuery(mainSql, paraMap);
	}

	@Override
	public boolean updateUserRole(String[] checkItems, String userId)  throws Exception{
		if(StringUtils.isBlank(userId)){
			return false;
		}
		//删除所的旧的角色资源.
		String delSql = "delete s_user_role sur where sur.user_id = :userId";
		Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("userId", userId);
		baseDAO.exeQuery(delSql, paraMap);
		//添加用户角色
		if(checkItems.length > 0){
			SRoleUser roleUser = null;
			String str = null;
			for (int i = 0; i < checkItems.length; i++) {
				str = checkItems[i];
				if(StringUtils.isNotBlank(str)){
					roleUser = new SRoleUser();
					roleUser.setId(UUIDUtil.makeUUID());
					roleUser.setLastModifyTime(DateUtil.formatDate());
					roleUser.setUserId(userId);
					roleUser.setRoleId(str.trim());
					baseDAO.saveOrUpdate(roleUser);
				}
			}
		}
		return true;
	}
}
